<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/30
 * Time: 15:41
 */

namespace Admin\Controller;


use Think\Controller;

class JsglController extends AdminController
{
    //对父类构造函数的补充
    public function _myinitialize()
    {
        $this->db = M('khzy');
        $this->db1 = M('dkzy');
        $this->db2 = M('cszy');
    }

    //www.glxt.com/admin/jsgl/lst
    public function lst()
    {

        $session_where=I('session_where');//不为空，则表明是从add/edit/del中跳转过来的，刚查询条件从session中取。
        if(empty($session_where)){
            //查询条件
            $where['status'] = array('gt', 0);
            $month = I('month');
            $year = I('year');
            if (!empty($month) && !empty($year)) { //所属月份
                $ssyf = $year . $month;
                $where['yf'] = $ssyf;
            }

            if (count($where) === 1) { //此时查询条件仅为status=1，表示没有别的查询条件，则默认查询条件为：只显示上月的数据
                $last_m = date("Ym",strtotime(date("Ym01",time()))-8600);;//默认情况下显示上月的数据
                $where['yf'] = $last_m;
            }
            //将查询条件存入session中，方便更新时跳转
            session(CONTROLLER_NAME."where",$where);
        }else{
            $where=session(CONTROLLER_NAME."where");
        }

        $yf=session('yf',$where['yf']);
        $lists = $this->db->where($where)->select();//所有符合条件的数据
        $khmc_arr = array_unique(array_column($lists, "khmc"));//所有符合条件的去重后的客户名称
        //将数组改造成：客户名称相同的放在一个数组中，并且该数组的键为客户名称
        $list_arr = array();
        foreach ($lists as $v) {
            foreach ($khmc_arr as $v1) {
                if ($v1 === $v['khmc']) {
                    $list_arr[$v1][] = $v;
                }
            }
        }
        unset($v);
//        print_r($list_arr);
        //建一个新的字段日期区间，为计费日期-结束日期
        $list_arrn = array();
        foreach ($list_arr as $k => $v) {
            foreach ($v as $v1) {
                $v1['rqqj'] = $v1['jfrq'] . "-" . $v1['jsrq'];
                $list_arrn[$k][] = $v1;
            }
        }
        unset($k, $v, $v1);
        //将数组改造成相同时间区间放在一个数组中的四维数组
        $list_arra = array();
        foreach ($list_arrn as $k => $v) {
            $list_arrb = array();
            foreach ($v as $v1) {
                $list_arrb[$v1['rqqj']][] = $v1;
            }
            $list_arra[$k] = $list_arrb;

        }
        unset($k, $v);
        ////求出同一个客户相同日期区间下使用量的总和
        foreach ($list_arra as $k => $v) {
            foreach ($v as $k1 => $v1) {
                $gs_sum = array_sum(array_column($v1, 'gs'));
                $list_arra[$k][$k1]['gs_sum'] = $gs_sum;
                $list_arra[$k][$k1]['ts_sum'] = (strtotime($v1[0]['jsrq']) - strtotime($v1[0]['jfrq'])) / 86400+1;
            }
        }
        unset($k, $v);
        //拼出最终分配到页面的数组
//        print_r($list_arra);die;
        $list_arrres = array();
        foreach ($list_arra as $k => $v) {
            foreach ($v as $k1 => $v1) {
                $k2 = $v1['ts_sum'];
                $kh_ids_arr = array_column($v1, 'id');//同一个客户下同一个时间区间的所有khzy表的id
                $kh_ids = implode(',', $kh_ids_arr);
                $kh_ids = trim($kh_ids, ',');
                $list_arrres[$k][$k2]['kh_ids'] = $kh_ids;
                $list_arrres[$k][$k2]['khmc'] = $v1[0]['khmc'];//客户名称
                $list_arrres[$k][$k2]['ywqy'] = $v1[0]['ywqy'];//业务区域
                $list_arrres[$k][$k2]['yyslx'] = $v1[0]['yyslx'];//运营商类型
                $list_arrres[$k][$k2]['rqqj'] = $v1[0]['rqqj'];//时间区间
                $list_arrres[$k][$k2]['ftth_sy'] = $v1[0]['ftth_sy'];//ftth使用
                $list_arrres[$k][$k2]['ts_sum'] = $v1['ts_sum'];//所用总天数
                $list_arrres[$k][$k2]['gs_sum'] = $v1['gs_sum'];//所用总量
            }
        }
        foreach ($list_arrres as &$v) {
            $sort = array_column($v, 'ts_sum');
            array_multisort($sort, SORT_DESC, $v);//对每一个用户下不同时间区域内按所用总量从大到小排序
        }
        unset($v);
        //以上的$list_arrres为客户资源部分，接下来加入带宽资源和传输资源
        $lsts = array();
        // echo "<pre>";
        // var_dump($list_arrres);die;
        if(!empty($list_arrres)){
            foreach ($list_arrres as $k => $v) { //$k为客户名称
                //取出当前客户的dkzy_ids和cszy_ids
                $where['khmc'] = $k;
                $dkzy_ids = $this->db->where($where)->getField('dkzy_ids');
                $cszy_ids = $this->db->where($where)->getField('cszy_ids');

                //根据dkzy_ids和cszy_ids查出当前客户有哪些带宽和传输
                if(!empty($dkzy_ids)){
                    $map['id'] = array('in', $dkzy_ids);
                    $dkzy_arr = $this->db1->where($map)->select();
                }else{
                    $dkzy_arr=array();
                }

                if(!empty($cszy_ids)){
                    $map1['id'] = array('in', $cszy_ids);
                    $cszy_arr = $this->db2->where($map1)->select();
                }else{
                    $cszy_arr=array();
                }

                //将当前客户的带宽和传输放入当前客户所属的数组
                $lsts[$k]['khzy'] = $v;
                $lsts[$k]['khzy'][0]['all_kh_ids'] = implode(",", array_column($v, 'kh_ids'));
                $lsts[$k]['dkzy'] = $dkzy_arr;
                $lsts[$k]['cszy'] = $cszy_arr;

            }
        }

        $this->assign('lsts', $lsts);
        $this->display();

    }

    //由ajax得到带宽资源的全部数据 www.glxt.com/admin/jsgl/get_dkzy
    public function get_dkzy()
    {
        $current_m = session('yf');
        $where['sssj'] = array('between', array($current_m . '01', $current_m . '31'));
        $where['status'] = array('gt', 0);
        $lists = $this->db1->where($where)->select();

        //将数组改造成将相同带宽供应商放在同一个数组，形成三维数组
        $dkgys_arr = array_unique(array_column($lists, 'dkgys'));
        $list_arr = array();
        foreach ($lists as $v) {
            foreach ($dkgys_arr as $v1) {
                if ($v['dkgys'] === $v1) {
                    $list_arr[$v1][] = $v;
                }
            }
        }
        unset($k, $v, $k1, $v1);
//        print_r($list_arr);
        $this->ajaxReturn($list_arr);
    }

    //由ajax得到传输供应商的全部数据 www.glxt.com/admin/jsgl/get_cszy
    public function get_cszy()
    {

        $current_m = session('yf');
        $where['sssj'] = array('between', array($current_m . '01', $current_m . '31'));
        $where['status'] = array('gt', 0);
        $lists = $this->db2->where($where)->select();

        //将数组改造成将相同传输供应商放在同一个数组中三维数组
        $csgys_arr = array_unique(array_column($lists, 'csgys'));
        $list_arr = array();
        foreach ($lists as $v) {
            foreach ($csgys_arr as $v1) {
                if ($v1 === $v['csgys']) {
                    $list_arr[$v1][] = $v;
                }
            }
        }
        unset($v, $v1);
        $this->ajaxReturn($list_arr);
    }

    //为每位客户匹配带宽资源和传输资源 www.glxt.com/admin/jsgl/kh_dk_cs_save
    public function kh_dk_cs_save()
    {
//        echo "<pre>";
//        print_r(I());die;
        $kh_ids = I('kh_ids');//客户ids
        $dk_ids = I('dk_ids');//带宽ids
        $cs_ids = I('cs_ids');//传输ids
        if (!empty($kh_ids)) {
            $kh_ids_arr = explode(',', $kh_ids);
            $map['id'] = $kh_ids_arr[0];//因为同一个客户的dkzy_ids或cszy_ids都是相同的，所以取khzy表的第一个id就作为查找目前该用户的带宽ids的条件
            $where['id'] = array('in', $kh_ids);

            if (!empty($dk_ids)) {
                $current_dkzy_ids = $this->db->where($map)->getField('dkzy_ids');//目前该用户的带宽ids
                if (!empty($current_dkzy_ids)) { //当数据表里面已经有数据时
                    $current_dkzy_ids_arr = explode(',', $current_dkzy_ids);
                    $get_dkzy_ids_arr = explode(',', $dk_ids);
                    $last_dkzy_ids_arr = array_unique(array_merge($current_dkzy_ids_arr, $get_dkzy_ids_arr));
                    $dk_ids = implode(',', $last_dkzy_ids_arr);
                }
                $this->db->where($where)->setField('dkzy_ids', $dk_ids);
            }
            if (!empty($cs_ids)) {
                $current_cs_ids = $this->db->where($map)->getField('cszy_ids');//目前该用户的带宽ids
                if (!empty($current_cs_ids)) {
                    $current_cs_ids_arr = explode(',', $current_cs_ids);
                    $get_cs_ids_arr = explode(',', $cs_ids);
                    $last_cs_ids_arr = array_unique(array_merge($current_cs_ids_arr, $get_cs_ids_arr));
                    $cs_ids = implode(',', $last_cs_ids_arr);
                }
                $this->db->where($where)->setField('cszy_ids', $cs_ids);
            }
        }

        $this->redirect('lst',array("session_where"=>"session_where"));
    }

    //删除当前客户的带宽或者传输 www.glxt.com/admin/jsgl/dk_cs_del
    public function dk_cs_del()
    {
        $kh_ids = I('kh_ids');//客户ids
        $dk_id = I('dk_id');//带宽id
        $cs_id = I('cs_id');//传输id
        if (!empty($kh_ids)) {
            $kh_ids_arr = explode(',', $kh_ids);
            $map['id'] = $kh_ids_arr[0];//因为同一个客户的dkzy_ids或cszy_ids都是相同的，所以取khzy表的第一个id就作为查找目前该用户的带宽ids的条件
            $where['id'] = array('in', $kh_ids);

            //删除带宽
            if (!empty($dk_id)) {
                $current_dkzy_ids = $this->db->where($map)->getField('dkzy_ids');//目前该用户的带宽ids
                $current_dkzy_ids_arr = explode(',', $current_dkzy_ids);
                if (in_array($dk_id, $current_dkzy_ids_arr)) { //判断一下，防止这个带宽id是在地址栏乱输入的
                    $del_key = array_search($dk_id, $current_dkzy_ids_arr);
                    unset($current_dkzy_ids_arr[$del_key]);
                    $new_dkzy_ids = trim(implode(',', $current_dkzy_ids_arr), ',');
                    $this->db->where($where)->setField('dkzy_ids', $new_dkzy_ids);
                }
            }

            //删除传输
            if (!empty($cs_id)) {
                $current_cszy_ids = $this->db->where($map)->getField('cszy_ids');//目前该用户的带宽ids
                $current_cszy_ids_arr = explode(',', $current_cszy_ids);
                if (in_array($cs_id, $current_cszy_ids_arr)) { //判断一下，防止这个传输id是在地址栏乱输入的
                    $del_key = array_search($cs_id, $current_cszy_ids_arr);
                    unset($current_cszy_ids_arr[$del_key]);
                    $new_cszy_ids = trim(implode(',', $current_cszy_ids_arr), ',');
                    $this->db->where($where)->setField('cszy_ids', $new_cszy_ids);
                }
            }
        }
        $this->redirect('lst',array("session_where"=>"session_where"));
    }

    //为带宽资源增加使用百分比
    public function dkzy_sy()
    {
        $dk_id = I('dk_id');//带宽id
        $sy = I('sy', '', 'trim');

        if ($sy > 100) $sy = 100;//使用百分比的最大值限定为100
        if (!empty($sy)) {
            $where['id'] = $dk_id;
            $ys=$this->db1->where($where)->getField('ys');

            $data['sy']=$sy;
            $data['sjcb']=$ys*$sy/100;//计算出实际成本
            $this->db1->where($where)->setField($data);
        }

        $this->redirect('lst',array("session_where"=>"session_where"));
    }

    //为ftth增加使用百分比
    public function ftth_sy()
    {

        $kh_ids = I('kh_ids');
        $ftth_sy = I('ftth_sy', '', 'trim');

        if ($ftth_sy > 100) $ftth_sy = 100;
        if (!empty($ftth_sy)) {
            $where['id'] = array('in', $kh_ids);
            $this->db->where($where)->setField('ftth_sy', $ftth_sy);
        }
        $this->redirect('lst',array("session_where"=>"session_where"));
    }
}